﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RzCorportativo.Views
{
    public partial class Idiomas : System.Web.UI.UserControl
    {
        private Lib.Vaga CurrentVaga { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Nome.Attributes.Add("url", this.ResolveUrl("~/Views/Cadastros/Curriculo.aspx/ListarIdiomas"));
                if (AppSession.Login != null && AppSession.Login.TipoLogin == Lib.TipoLogin.Empresa)
                {
                    if (CurrentVaga == null)
                        return;
                    List<Lib.Idioma> lista = CurrentVaga.ListarIdiomas();
                    foreach (var item in lista)
                    {
                        var row = new System.Web.UI.HtmlControls.HtmlTableRow();
                        row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                        {
                            InnerText = item.Nome
                        });
                        row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                        {
                            InnerText = "B".Equals(item.Nivel) ? "BÁSICO" : "I".Equals(item.Nivel) ? "INTERMEDIÁRIO" : "AVANÇADO"
                        });
                        row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                        {
                            InnerHtml = "<input type='button' id='btnRemove" + item.IdiomaId.ToString() +
                            "' onclick='RemoveItem(" + item.IdiomaId.ToString() +
                            "," + CurrentVaga.VagaId.ToString() +
                            ", this)' class='btn btn-danger' value='remover'/>"
                        });
                        row.Attributes.Add("idioma", item.IdiomaId.ToString());
                        this.gridIdiomas.Rows.Add(row);
                    }
                }
                else
                {
                    var lista = Lib.IdiomaUsuario.Listar(new Lib.IdiomaUsuario()
                    {
                        UsuarioId = (AppSession.Login.Usuario ?? new Lib.Usuario()).UsuarioId
                    });
                    foreach (var item in lista)
                    {
                        var row = new System.Web.UI.HtmlControls.HtmlTableRow();
                        row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                        {
                            InnerText = (item.Idioma ?? new Lib.Idioma()).Nome
                        });
                        row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                        {
                            InnerText = "B".Equals(item.Nivel) ? "BÁSICO" : "I".Equals(item.Nivel) ? "INTERMEDIÁRIO" : "AVANÇADO"
                        });
                        row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                        {
                            InnerHtml = "<input type='button' id='btnRemove" + item.IdiomaId.ToString() +
                            "' onclick='RemoveItem(" + item.IdiomaId.ToString() +
                            "," + (AppSession.Login.Usuario ?? new Lib.Usuario()).UsuarioId +
                            ", this)' class='btn btn-danger' value='remover'/>"
                        });
                        row.Attributes.Add("idioma", item.IdiomaId.ToString());
                        this.gridIdiomas.Rows.Add(row);
                    }
                }
            }
        }

        internal void LoadModel(Lib.Vaga vaga)
        {
            this.CurrentVaga = vaga;
        }

        public Lib.Idioma Model
        {
            get
            {
                Lib.Idioma idioma = new Lib.Idioma();
                idioma.Nome = Nome.Text;
                idioma.Nivel = Nivel.SelectedValue;

                return idioma;
            }
        }
    }
}